<?php

namespace app\common\model\xiluhouse;

use think\Model;
use traits\model\SoftDelete;

class ChatMsg extends Model
{

    use SoftDelete;

    // 表名
    protected $name = 'xiluhouse_chat_msg';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'content_text',
        'content_text2',
        'createtime_text',
        'chat_type_text',
        'is_read_text',
        'see_type_text',
    ];
    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? time_tran($value) : $value;
    }
    // 处理消息 如果是图片、房源，需要全路径
    public function getContentTextAttr($value, $data)
    {
        $chat_type = isset($data['chat_type']) ? $data['chat_type'] : 1;
        $content = $value ? $value : (isset($data['content']) ? $data['content'] : '');
        if ($chat_type == 2) {
            $content = cdnurl($content,true);
        } else if ($chat_type == 3) {
            $content = json_decode($content, true);
            $content['image_text'] = isset($content['image']) && $content['image'] ? cdnurl($content['image'],true) : '';
        }else if ($chat_type == 4) {
            $content = json_decode($content, true);
            $content['image_text'] = isset($content['image']) && $content['image'] ? cdnurl($content['image'],true) : '';
        }else if ($chat_type == 5) {
            $content = json_decode($content, true);
            $content['image_text'] = isset($content['image']) && $content['image'] ? cdnurl($content['image'],true) : '';
        }
        return $content;
    }
    // 处理消息 如果是图片、房源，需要全路径
    public function getContentText2Attr($value, $data)
    {
        $chat_type = isset($data['chat_type']) ? $data['chat_type'] : 1;
        $content = $value ? $value : (isset($data['content']) ? $data['content'] : '');
        $arr = ['[图片]', '[房源]','[租房]','[二手房]'];
        if ($chat_type != 1) {
            $content = $arr[$chat_type - 2];
        }
        return $content;
    }

    public function getChatTypeList()
    {
        return ['1' => __('文字'), '2' => __('图片'), '3' => __('房源'),'4'=>('租房'),'5'=>__('二手房')];
    }

    public function getIsReadList()
    {
        return ['0' => __('否'), '1' => __('是')];
    }

    // 可见状态:0=正常,1=发送人不可见,2=接收人不可见,3=发送人撤回消息(双方都不可见),4=双方都删除记录(双方都不可见)
    public function getSeeTypeList()
    {
        return ['0' => __('正常'), '1' => __('发送人不可见'), '2' => __('接收人不可见'), '3' => __('发送人撤回消息(双方都不可见)'), '4' => __('双方都删除记录(双方都不可见)')];
    }

    public function getChatTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['chat_type']) ? $data['chat_type'] : '');
        $list = $this->getChatTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getIsReadTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_read']) ? $data['is_read'] : '');
        $list = $this->getIsReadList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getSeeTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['see_type']) ? $data['see_type'] : '');
        $list = $this->getSeeTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function user()
    {
        return $this->belongsTo('User', 'send_user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function senduser()
    {
        return $this->belongsTo('User', 'send_user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function receiveuser()
    {
        return $this->belongsTo('User', 'receive_user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
